Skip to content

Disallow empty parameter clauses in extension definition #23143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 13, 2025

Conversation

DolphinChips
Copy link
Contributor

@DolphinChips DolphinChips commented May 12, 2025

docs/_docs/internals/syntax.md doesn't seem to indicate that this a valid syntax. Before this patch the following extension:

extension(x: Any)() // error
  def f = 42

...could be called as 1.f(). After this patch extension above raises a syntax error and compiler recovers by ignoring empty clause altogether.

Side-note: recovery described above happens in extension(), termParamClause still returns an empty list.

Copy link
Contributor

@som-snytt som-snytt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice catch! I miss the puzzlers page.

@som-snytt
Copy link
Contributor

Just a footnote on the second parameter list to a collective extension.

scala> extension (s: String)(using z: Int) { def f(i: Int) = s*(i*z); def g_:(i: Int) = (s*(i*z)).reverse }
def f(s: String)(using z: Int)(i: Int): String
def g_:(i: Int)(s: String)(using z: Int): String

scala> "xyz" f 5
-- [E008] Not Found Error: -----------------------------------------------------
1 |"xyz" f 5
  |^^^^^^^
  |value f is not a member of String.
  |An extension method was tried, but could not be fully constructed:
  |
  |    f("xyz")(/* missing */summon[Int])
  |
  |    failed with:
  |
  |        No given instance of type Int was found for parameter z of method f
1 error found

scala> "xyz" g_:  5
-- [E172] Type Error: ----------------------------------------------------------
1 |"xyz" g_:  5
  |            ^
  |     No given instance of type Int was found for parameter z of method g_:
1 error found

scala> ("xyz" g_: 5)(using 2)
val res10: String = zyxzyxzyxzyxzyxzyxzyxzyxzyxzyx

For f, there is no syntax for infix using. (The limitation for supplying type args is mentioned in the reference.)

@som-snytt som-snytt merged commit 004579e into scala:main May 13, 2025
29 checks passed
@som-snytt
Copy link
Contributor

The syntax error is appropriately terse and corresponds to the explicit arg case. Probably anyone attempting to supply a param list in this position knows what they're doing or expects to be confused.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants